﻿using BeerXMLWPF.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BeerXMLWPF
{
    /// <summary>
    /// Interaction logic for EditYeast.xaml
    /// </summary>
    public partial class EditYeast : Window
    {
        List<string> lstComboboxForm;
        List<string> lstComboboxType;
        private string p;
        public EditYeast()
        {
            InitializeComponent();
            lstComboboxType = new List<string>();
            lstComboboxForm = new List<string>();
            lstComboboxType.Add("Ale");
            lstComboboxType.Add("Lager");
            lstComboboxType.Add("Wheat");
            lstComboboxType.Add("Wine");
            lstComboboxType.Add("Champagne");
            lstComboboxForm.Add("Liquid");
            lstComboboxForm.Add("Dry");
            lstComboboxForm.Add("Slant");
            lstComboboxForm.Add("Culture");
            cbType.ItemsSource = lstComboboxType;
            cbForm.ItemsSource = lstComboboxForm;
            LoadContent("new");
        }

        public EditYeast(string p)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            lstComboboxType = new List<string>();
            lstComboboxForm = new List<string>();
            lstComboboxType.Add("Ale");
            lstComboboxType.Add("Lager");
            lstComboboxType.Add("Wheat");
            lstComboboxType.Add("Wine");
            lstComboboxType.Add("Champagne");
            lstComboboxForm.Add("Liquid");
            lstComboboxForm.Add("Dry");
            lstComboboxForm.Add("Slant");
            lstComboboxForm.Add("Culture");
            cbType.ItemsSource = lstComboboxType;
            cbForm.ItemsSource = lstComboboxForm;
            LoadContent(p);
        }

        private void LoadContent(string s)
        {
            if (s == "new")
            {
                Global.yeast = new Yeast();
                cbType.SelectedItem = "Ale";
                cbForm.SelectedItem = "Liquid";
            }
            else
            {
                txtName.Text = Global.yeast.Name;
                cbForm.SelectedItem = Global.yeast.Form;
                txtAmount.Text = Global.yeast.Amount;
                cbType.SelectedItem = Global.yeast.Type;

            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("Please enter yeast name");
                return;
            }
            try
            {
                double amount = double.Parse(txtAmount.Text);
                if (amount <= 0)
                {
                    MessageBox.Show("Amount must be a double and greater than 0");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Amount must be a double and greater than 0");
                return;
            }

            Global.yeast.Name = txtName.Text;
            Global.yeast.Amount = double.Parse(txtAmount.Text).ToString();
            Global.yeast.Display_Amount = Global.yeast.Amount + " kg";
            Global.yeast.Form = cbForm.SelectedItem.ToString();
            Global.yeast.Type = cbType.SelectedItem.ToString();
            Global.yeast.Version = "1";
            Global.OK = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
